package com.chujiekeji.test.activity.chat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chujiekeji.test.R;
import com.chujiekeji.test.account.AccountHelper;
import com.chujiekeji.test.activity.LoginActivity;
import com.chujiekeji.test.activity.chat.adapter.MsgAdapter;
import com.chujiekeji.test.activity.chat.model.CMessage;
import com.chujiekeji.test.activity.chat.model.Callback;
import com.chujiekeji.test.activity.chat.model.MsgType;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private ListView msgListView;
    private EditText inputText;
    private Button send;
    private MsgAdapter adapter;
    private TextView etAccount;

    private String helpnum;
    private List<CMessage> msgList = new ArrayList<CMessage>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intentFrom = getIntent();
        String realname = intentFrom.getStringExtra("nickName");
        helpnum = intentFrom.getStringExtra("helpNum");
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.hide();

        setContentView(R.layout.activity_chat);
//        if (AppCache.getMyInfo() == null) {
//            Intent intent = new Intent(this, LoginActivity.class);
//            startActivity(intent);
//            finish();
//            return;
//        }
        initMsgs();
        adapter = new MsgAdapter(ChatActivity.this, R.layout.msg_item, msgList);
        inputText = (EditText)findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        msgListView = (ListView)findViewById(R.id.msg_list_view);
        etAccount = findViewById(R.id.toUser);
        etAccount.setText(realname);
        msgListView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String myAccount = AccountHelper.getUser().getHelpNum();
                String content = inputText.getText().toString();
                if(!"".equals(content)) {
                    final CMessage message = new CMessage();
                    message.setFrom(myAccount);
                    message.setTo(helpnum);
                    message.setType(MsgType.TEXT);
                    message.setContent(content);
                    message.setSource(CMessage.SOURCE_SEND);
                    msgList.add(message);
                    adapter.notifyDataSetChanged();
                    AppCache.getService().sendMsg(message, new Callback<Void>() {
                        @Override
                        public void onEvent(Context context, int code, String msg, Void aVoid) {
                            if (code == 200) {
                                msgListView.setSelection(msgList.size());
                                inputText.setText("");
                                Toast.makeText(getApplicationContext(),"发送成功",Toast.LENGTH_SHORT);
                            } else {
                                Toast.makeText(getApplicationContext(),"发送成功",Toast.LENGTH_SHORT);
                            }
                        }
                    });

                }
            }
        });
        AppCache.getService().setReceiveMsgCallback(receiveMsgCallback);
    }

    private Callback<CMessage> receiveMsgCallback = new Callback<CMessage>() {
        @Override
        public void onEvent(Context context,int code, String msg, CMessage message) {
            if (message.getFrom().equals(helpnum)){
                msgList.add(message);
                adapter.notifyDataSetChanged();
            }
//            terminal.append("[接收]" + message.getFrom() + ":" + message.getContent());
//            terminal.append("\n");
        }
    };

    private void initMsgs() {
//        CMessage msg1 = new CMessage("Hello, how are you?", CMessage.SOURCE_RECEIVED);
//        msgList.add(msg1);
//        CMessage msg2 = new CMessage("Fine, thank you, and you?", CMessage.SOURCE_SEND);
//        msgList.add(msg2);
//        CMessage msg3 = new CMessage("I am fine, too!", CMessage.SOURCE_RECEIVED);
//        msgList.add(msg3);
    }
}
